package cn.edu.jju.stepcounter2.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import cn.edu.jju.stepcounter2.SCApp;
import cn.edu.jju.stepcounter2.bean.DayData;
import cn.edu.jju.stepcounter2.main.model.MainModel;
import cn.edu.jju.stepcounter2.main.model.MainModelImpl;
import cn.edu.jju.stepcounter2.main.model.OnDataGetListener;
import cn.edu.jju.stepcounter2.utils.GetDate;
import cn.edu.jju.stepcounter2.utils.SPUtils;

import static android.util.Log.i;

public class AlarmService extends Service {

    private List<DayData> dayDatas;

    public AlarmService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        i("@@@@", "onCreateservice" + "newDay");
//        SPUtils.put(getApplicationContext(),"date", "2016");
//        DayData dayData = new DayData();
//        dayData.setExpectStep((String) SPUtils.get(this,"expectStep","5000"));
//        dayData.setDate("2016");
//        dayData.setSteps("0");
//        dayData.setCalConsume("0");
//        try {
//            x.getDb(SCApp.daoConfig).saveBindingId(dayData);
//            Log.i("@@@", "newDay" );
//        } catch (DbException e) {
//            e.printStackTrace();
//        }
//        IntentFilter filter =new IntentFilter("newDay");
//        sendBroadcast(new Intent("newDay"));


    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        i("@@@@", "onStartCommand" + "newDay");

        SPUtils.put(getApplicationContext(), "date", GetDate.get());
        DayData dayData = new DayData();
        dayData.setExpectStep((String) SPUtils.get(this, "expectStep", "5000"));
        dayData.setDate(GetDate.get());
        dayData.setSteps("0");
        dayData.setCalConsume("0");
        dayDatas = new ArrayList<>();
        MainModel mainModel = new MainModelImpl();
        mainModel.getData(new OnDataGetListener() {
            @Override
            public void onDataGetSuccess(List<DayData> dayDataList) {
                dayDatas = dayDataList;
            }

            @Override
            public void onDataGetFailed(String error) {

            }
        });
        boolean flag = true;
        for (DayData d:
             dayDatas) {
            if (d.getDate().equals(dayData.getDate())){
                flag = false;
            }
        }
        if (flag){
            try {
                x.getDb(SCApp.daoConfig).saveBindingId(dayData);
                Log.i("@@@", "newDay");
            } catch (DbException e) {
                e.printStackTrace();
            }
            sendBroadcast(new Intent("newDay"));
        }
        return super.onStartCommand(intent, flags, startId);
    }
}
